<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="名称">
        <el-input v-model="banner.title" />
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number
          v-model="banner.sort"
          controls-position="right"
          :min="0"
        />
      </el-form-item>
      

      <el-form-item label="跳转链接">
        <el-input v-model="banner.linkUrl" :rows="10"  />
      </el-form-item>

      <!-- 讲师头像：TODO -->
      <el-form-item label="banner图片">
        <!-- 头衔缩略图 -->
        <pan-thumb :image="banner.imageUrl" />
        <!-- 文件上传按钮 -->
        <el-button
          type="primary"
          icon="el-icon-upload"
          @click="imagecropperShow = true"
          >上传图片
        </el-button>

        <!--
      v-show：是否显示上传组件
      :key：类似于id，如果一个页面多个图片上传控件，可以做区分
      :url：后台上传的url地址
      @close：关闭上传组件
      @crop-upload-success：上传成功后的回调 
        <input type="file" name="file"/>
      -->
        <image-cropper
          v-show="imagecropperShow"
          :width="1920"
          :height="535"
          :key="imagecropperKey"
          :url="BASE_API + '/eduoss/uploadPic'"
          field="file"
          @close="close"
          @crop-upload-success="cropSuccess"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import bannerApi from "@/api/edu/banner";
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'

export default {
  components: { ImageCropper, PanThumb },
  data() {
    return {
      banner: {
        title: "",
        sort: 0,
        linkUrl: "",
        imageUrl: "",
      },
       //上传弹框组件是否显示
      imagecropperShow:false,
      imagecropperKey:0,//上传组件key值
      BASE_API:process.env.BASE_API, //获取dev.env.js里面地址
      saveBtnDisabled:false  // 保存按钮是否禁用,
    };
  },

  created() {
    this.init();
  },

  watch: {
    $route(to, from) {
      //监听路由变化事件 固定写法

      this.init();
    },
  },

  methods: {
    close() { //关闭上传弹框的方法
        this.imagecropperShow=false
        //上传组件初始化
        this.imagecropperKey = this.imagecropperKey+1
    },
    //上传成功方法
    cropSuccess(data) {
      this.imagecropperShow=false
      //上传之后接口返回图片地址
      this.banner.imageUrl = data
      this.imagecropperKey = this.imagecropperKey+1
    },
    init() {
      if (this.$route.params && this.$route.params.id) {
        //路径有id就回显

        console.log("回显数据.....");
        const id = this.$route.params.id;

        this.getInfo(id);
      } else {
        //没有id就清空数据
        console.log("清空数据.....");
        this.banner = {};
      }
    },

    saveOrUpdate() {
      if (this.banner.id) {
        this.update(this.banner);
      } else {
        this.save(this.banner);
      }
    },

    //保存的方法
    save() {
      bannerApi.addBanner(this.banner).then((response) => {
        //提示添加成功
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //跳转
        this.$router.push({ path: "/edu/banner/list" });
      });
    },

    //数据回显
    getInfo(id) {
      bannerApi.getBannerById(id).then((response) => {
        console.log(response.data);
        this.banner = response.data;
      });
    },

    //修改
    update() {
      bannerApi.updateBanner(this.banner).then((response) => {
        //提示修改成功
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //跳转
        this.$router.push({ path: "/edu/banner/list" });
      });
    },
  },
};
</script>
